package com.jie.langlangmall.portal.service.impl;

import com.jie.langlangmall.common.constant.MessageConstant;
import com.jie.langlangmall.common.exception.ApiException;
import com.jie.langlangmall.mapper.PmsBrandMapper;
import com.jie.langlangmall.model.PmsBrand;
import com.jie.langlangmall.model.UmsMember;
import com.jie.langlangmall.portal.domain.MemberBrandAttention;
import com.jie.langlangmall.portal.repository.MemberBrandAttentionRepository;
import com.jie.langlangmall.portal.service.MemberAttentionService;
import com.jie.langlangmall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author Jie
 * @Date 2024/11/27
 * @Email 3217998214@qq.com
 * @Description 用户关注
 */
@Service
public class MemberAttentionServiceImpl implements MemberAttentionService {

    @Autowired
    private PmsBrandMapper brandMapper;
    @Autowired
    private MemberBrandAttentionRepository memberBrandAttentionRepository;
    @Autowired
    private UmsMemberService memberService;

    @Override
    public void add(MemberBrandAttention memberBrandAttention) {
        if (memberBrandAttention.getBrandId() == null) {
            throw new ApiException(MessageConstant.SUBMIT_PARAM_ERROR);
        }
        UmsMember member = memberService.getCurrentMember();
        memberBrandAttention.setMemberId(member.getId());
        memberBrandAttention.setMemberNickname(member.getNickname());
        memberBrandAttention.setMemberIcon(member.getIcon());
        memberBrandAttention.setCreateTime(new Date());
        MemberBrandAttention findAttention = memberBrandAttentionRepository.findByMemberIdAndBrandId(memberBrandAttention.getMemberId(), memberBrandAttention.getBrandId());
        if (findAttention == null) {
            PmsBrand brand = brandMapper.selectByPrimaryKey(memberBrandAttention.getBrandId());
            if (brand == null) {
                throw new ApiException("您关注的品牌不存在");
            }
            memberBrandAttention.setBrandCity(null);
            memberBrandAttention.setBrandName(brand.getName());
            memberBrandAttention.setBrandLogo(brand.getLogo());
            memberBrandAttentionRepository.save(memberBrandAttention);
        }
    }

    @Override
    public void delete(Long brandId) {
        UmsMember member = memberService.getCurrentMember();
        memberBrandAttentionRepository.deleteByMemberIdAndBrandId(member.getId(), brandId);
    }

    @Override
    public Page<MemberBrandAttention> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return memberBrandAttentionRepository.findByMemberId(member.getId(), pageable);
    }

    @Override
    public MemberBrandAttention detail(Long brandId) {
        UmsMember member = memberService.getCurrentMember();
        return memberBrandAttentionRepository.findByMemberIdAndBrandId(member.getId(), brandId);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        memberBrandAttentionRepository.deleteAllByMemberId(member.getId());
    }
}
